﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;

namespace CommunitySystem
{
    public partial class ZhaoLuJiHuaTitleWindow : ChildWindow
    {
        private int Year;
        private ZhaoLuService.ZhaoLuServiceClient Client = new ZhaoLuServiceClient();
        public ZhaoLuJiHuaTitleWindow(int year)
        {
            InitializeComponent();
            Year = year;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ZhaoLuJiHuaModel model = new ZhaoLuJiHuaModel();
            model.Year = Year;
            model.Name = zhiwei.Text;
            Client.InsertZhaoLuJiHuaTitleCompleted += new EventHandler<InsertZhaoLuJiHuaTitleCompletedEventArgs>(Client_InsertZhaoLuJiHuaTitleCompleted);
            Client.InsertZhaoLuJiHuaTitleAsync(model);
        }

        private int ShengChengID = 0;
        void Client_InsertZhaoLuJiHuaTitleCompleted(object sender, InsertZhaoLuJiHuaTitleCompletedEventArgs e)
        {
            if(e.Result != 0)
            {
                ShengChengID = e.Result;
                ZhaoLuJiHuaWindow win = new ZhaoLuJiHuaWindow(0,e.Result, Year, zhiwei.Text,false);
                win.Show();
                win.Closed += new EventHandler(win_Closed);
                this.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("新增失败！");
            }

        }

        void win_Closed(object sender, EventArgs e)
        {
            if (((ZhaoLuJiHuaWindow)sender).DialogResult == true)
                this.DialogResult = true;
            else
            {
                Client.DeleteZhaoLuJiHuaCompleted -= new EventHandler<DeleteZhaoLuJiHuaCompletedEventArgs>(Client_DeleteZhaoLuJiHuaCompleted);
                Client.DeleteZhaoLuJiHuaCompleted += new EventHandler<DeleteZhaoLuJiHuaCompletedEventArgs>(Client_DeleteZhaoLuJiHuaCompleted);
                Client.DeleteZhaoLuJiHuaAsync(ShengChengID);
            }
        }

        void Client_DeleteZhaoLuJiHuaCompleted(object sender, DeleteZhaoLuJiHuaCompletedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}
